console.log('process_env::', process.env.NODE_ENV);
type Environment = 'production' | 'development';
const env: Environment =
  process.env.API_ENV || process.env.NODE_ENV || 'development';

interface Config {
  // API: {
  //   BASE_HOST: string;
  // };
  ENV: Environment;
  OTH_CONFIG: any;
}
type UltimateConfig = {
  // API: {
  //   BASE_HOST: string;
  //   RECRUIT_HOST: string
  //   RENT_API: string;
  //   EDUCATION_HOST: string;
  // };
} & Config;

const originalConfig: Config = {
  get ENV() {
    return env;
  },
  OTH_CONFIG: {},
};

function getOwnPropertyDescriptors(obj) {
  const result: any = {};
  for (let key of Reflect.ownKeys(obj)) {
    result[key] = Object.getOwnPropertyDescriptor(obj, key);
  }
  return result;
}

export function useConfig<T>(
  builder: (originalConfig: Config) => T,
): Config & T {
  const customConfig = builder(originalConfig);
  const ultimateConfig = {} as Config & T;

  Object.defineProperties(
    ultimateConfig,
    getOwnPropertyDescriptors(originalConfig),
  );
  Object.defineProperties(
    ultimateConfig,
    getOwnPropertyDescriptors(customConfig),
  );

  return ultimateConfig;
}

const config: UltimateConfig = useConfig((coreConfig) => {
  const customConfig = {
    // API: {
    //   ...coreConfig.API,
    //   ...APISchema[coreConfig.ENV],
    // },
  };

  return customConfig;
});

export default config;
